//
//  XKTradePendingOrderCell.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/13.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKTradePendingOrderCell.h"


@interface XKTradePendingOrderCell ()
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UILabel *lineBottom;
@end
@implementation XKTradePendingOrderCell

#pragma mark - 自定义cell重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark - 取消委托
- (void)cancelOrder:(UIButton *)sender {
    if (self.CancelOrderBlock) {
        self.CancelOrderBlock([NSNumber numberWithInteger:self.userOrderModel.orders_id.integerValue]);
    }
}

#pragma mark - 创建控件
- (void)initSubviews {
    
    _typeLabel = [UILabel getLabWithText:@"----" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentLeft];
    
    _priceLabel = [UILabel getLabWithText:@"-.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    _numLabel = [UILabel getLabWithText:@"----" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    _timeLabel = [UILabel getLabWithText:@"--.--.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _cancelBtn.titleLabel.font = Font(14);
    [_cancelBtn setTitle:@"撤销" forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelOrder:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _lineBottom = [UILabel new];
    _lineBottom.backgroundColor = kBaseGrayColor;
    

    
    
    [self.contentView addSubview:self.typeLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.numLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.cancelBtn];
    [self.contentView addSubview:self.lineBottom];
    
    
    [self addConstraints];
}
#pragma mark - 添加约束
- (void)addConstraints {
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(22*Scale_W);
        make.top.mas_equalTo(self.contentView).offset(5*Scale_H);
        make.width.mas_equalTo(40*Scale_W);
    }];
    
    [self.lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1*Scale_H);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5*Scale_H);
        make.left.right.bottom.mas_equalTo(self.contentView).priorityHigh();
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.left.mas_equalTo(self.typeLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo((Main_Width - 160*Scale_W)/3);
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.left.mas_equalTo(self.priceLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo((Main_Width - 100*Scale_W)/3);
    }];

    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.typeLabel);
        make.left.mas_equalTo(self.numLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo((Main_Width - 160*Scale_W)/3);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.typeLabel);
        make.right.mas_equalTo(self.contentView).offset(-10*Scale_W);
    }];

}
- (void)setUserOrderModel:(XKUserOrdersModel *)userOrderModel{

    _userOrderModel = userOrderModel;
    
    if ([userOrderModel.type isEqualToString:@"buy"]) {
        self.typeLabel.text = @"买入";
        self.typeLabel.textColor = UIColorFromRGB(0xFD5251);
    } else if ([userOrderModel.type isEqualToString:@"sell"]) {
        self.typeLabel.text = @"卖出";
        self.typeLabel.textColor = UIColorFromRGB(0x4DB54F);
    }
    
    self.priceLabel.text = [NSString changeNumberFormat:userOrderModel.price];
    self.numLabel.text = [NSString changeNumberFormat:NSNumberFormatterDecimalStyle withNumber:[userOrderModel.num componentsSeparatedByString:@"."].firstObject];
    
    NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[userOrderModel.trade_time intValue]];
    if ([currentTime isToday]) {
        self.timeLabel.text = [NSString getTimeStringWithTimestamp:[userOrderModel.trade_time integerValue] withFormatter:@"HH:mm:ss"];// YY-MM-dd HH:mm:ss
    } else {
        self.timeLabel.text = [NSString getTimeStringWithTimestamp:[userOrderModel.trade_time integerValue] withFormatter:@"YY/MM/dd"];// YY-MM-dd HH:mm:ss
    }
    
}

@end
